package com.nbcio.iot.controller;

import cn.hutool.core.lang.tree.Tree;
import com.nbcio.common.core.domain.R;
import com.nbcio.common.core.validate.AddGroup;
import com.nbcio.common.core.validate.EditGroup;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.iot.domain.bo.IotProductCategoryBo;
import com.nbcio.iot.domain.vo.IotProductCategoryVo;
import com.nbcio.iot.service.IIotProductCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物联网产品分类控制器
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/category")
public class IotProductCategoryController extends BaseController {

    private final IIotProductCategoryService categoryService;

    /**
     * 查询产品分类列表
     */
    @GetMapping("/list")
    public TableDataInfo<IotProductCategoryVo> list(IotProductCategoryBo bo, PageQuery pageQuery) {
        List<IotProductCategoryVo> list = categoryService.selectCategoryList(bo);
        return TableDataInfo.build(list);
    }

    /**
     * 分页查询产品分类列表（兼容前端page请求）
     */
    @GetMapping("/page")
    public TableDataInfo<IotProductCategoryVo> page(IotProductCategoryBo bo, PageQuery pageQuery) {
        List<IotProductCategoryVo> list = categoryService.selectCategoryList(bo);
        return TableDataInfo.build(list);
    }

    /**
     * 查询产品分类树结构
     */
    @GetMapping("/tree")
    public R<List<Tree<Long>>> tree(IotProductCategoryBo bo) {
        List<Tree<Long>> trees = categoryService.selectCategoryTreeList(bo);
        return R.ok(trees);
    }

    /**
     * 获取产品分类详细信息
     */
    @GetMapping("/{categoryId}")
    public R<IotProductCategoryVo> getInfo(@PathVariable("categoryId") Long categoryId) {
        return R.ok(categoryService.selectCategoryById(categoryId));
    }

    /**
     * 新增产品分类
     */
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotProductCategoryBo bo) {
        if (!categoryService.checkCategoryNameUnique(bo)) {
            return R.fail("新增产品分类'" + bo.getCategoryName() + "'失败，分类名称已存在");
        }
        if (!categoryService.checkCategoryCodeUnique(bo)) {
            return R.fail("新增产品分类'" + bo.getCategoryName() + "'失败，分类编码已存在");
        }
        return toAjax(categoryService.insertCategory(bo));
    }

    /**
     * 修改产品分类
     */
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotProductCategoryBo bo) {
        if (!categoryService.checkCategoryNameUnique(bo)) {
            return R.fail("修改产品分类'" + bo.getCategoryName() + "'失败，分类名称已存在");
        }
        if (!categoryService.checkCategoryCodeUnique(bo)) {
            return R.fail("修改产品分类'" + bo.getCategoryName() + "'失败，分类编码已存在");
        }
        return toAjax(categoryService.updateCategory(bo));
    }

    /**
     * 删除产品分类
     */
    @DeleteMapping("/{categoryIds}")
    public R<Void> remove(@PathVariable Long[] categoryIds) {
        return toAjax(categoryService.deleteCategoryByIds(List.of(categoryIds)));
    }
} 